package com.hdy.framework.hibernate.jdbc;

import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

public class HibernateSpringDaoSupport extends HibernateDaoSupport{
	private JdbcTemplate springJdbcTemplate;
	private SqlMapClientTemplate ibatisSqlMapClientTemplate;
	public List getListByQureyStr(final String sqlString, final Vector pars,
            final int first, final int max) {
        HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
        //保证返回的是list
        List result = hibernateTemplate.executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                List ls = null;
                try {
                    Query qu = session.createQuery(sqlString);
                    if ((first >= 0) && (max >= 0)) {
                        qu.setFirstResult(first);
                        qu.setMaxResults(max);
                    }
                    if ((pars != null) && (pars.size() > 0)) {
                        for (int i = 0; i < pars.size(); i++) {
                            qu.setParameter(i, pars.get(i));
                        }
                    }
                    ls = qu.list();
                } catch (Exception e) {
                }
                return ls;
            }
        });
        return result;
    }
	public JdbcTemplate getSpringJdbcTemplate() {
//		开启不利于代码控制
//		if(springJdbcTemplate==null){
//			springJdbcTemplate=SpringContainer.getBean("springJdbcTemplate1", JdbcTemplate.class);
//		}
		return springJdbcTemplate;
	}

	public void setSpringJdbcTemplate(JdbcTemplate springJdbcTemplate) {
		this.springJdbcTemplate = springJdbcTemplate;
	}
	public SqlMapClientTemplate getIbatisSqlMapClientTemplate() {
		return ibatisSqlMapClientTemplate;
	}
	public void setIbatisSqlMapClientTemplate(
			SqlMapClientTemplate ibatisSqlMapClientTemplate) {
		this.ibatisSqlMapClientTemplate = ibatisSqlMapClientTemplate;
	}
}
